package com.psy.counseling.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.psy.counseling.entity.Ask;
import com.psy.counseling.entity.Situation;
import com.psy.counseling.mapper.AskMapper;
import com.psy.counseling.mapper.SituationMapper;
import com.psy.counseling.service.AskService;
import com.psy.counseling.service.SituationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psy.counseling.utils.Result;
import com.psy.counseling.vo.DeleteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author cxh
 * @since 2022-05-31
 */
@Service
public class SituationServiceImpl extends ServiceImpl<SituationMapper, Situation> implements SituationService {

    @Autowired
    private AskMapper askMapper;
    @Transactional
    @Override
    public Result<?> addSituation(Situation situation) {
        if (situation != null && situation.getAskId() != null) {
            LambdaQueryWrapper<Situation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Situation::getAskId, situation.getAskId());
            int number = this.count(wrapper);
//            LambdaQueryWrapper<Ask> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(Ask::getId,situation.getAskId());
            Ask ask = askMapper.selectById(situation.getAskId());
//            Integer total = askMapper.selectCount(queryWrapper);
            Integer total = ask.getTotal();
            if(number<total){
                situation.setNumber(number + 1);
                this.save(situation);
                return Result.success();
            }
        }
        return Result.error("204", "添加失败");
    }

    @Transactional
    @Override
    public Result<?> updateSituation(Situation situation) {
        if (situation.getId() != null) {
            this.updateById(situation);
            return Result.success();
        }
        return Result.error("204", "修改失败");
    }

    @Transactional
    @Override
    public Result<?> deleteSituation(DeleteVO deleteVO, Integer flag) {
        if (flag == 0 && deleteVO.getId() != null) {
            this.removeById(deleteVO.getId());
            return Result.success();
        }
        if (flag != 0 && deleteVO.getIds() != null) {
            this.removeByIds(deleteVO.getIds());
            return Result.success();
        }
        return Result.error("204", "删除失败");
    }

    @Transactional
    @Override
    public Result<Situation> getSituation(Long id) {
        if (id != null) {
            Situation situation = this.getById(id);
            return Result.success(situation);
        }
        return Result.error("204", "获取失败");
    }

    @Override
    public Integer getCount(Long askId) {
        LambdaQueryWrapper<Situation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Situation::getAskId, askId);
        int count = this.count(wrapper);
        return count;
    }

}
